﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_CatalogDtls : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IFieldBLL fieldBLL = new FieldBLL();
        private RecruitCatalogBLL catBLL = new RecruitCatalogBLL();
        private RecruitApplicantBLL appBLL = new RecruitApplicantBLL();

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            txtNo.Attributes.Add("onkeypress", "return isNumberKey(event)");

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }
            if (DataItem != null)
            {
                BindCatStatusCombo();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSequence.Text = DataBinder.Eval(DataItem, "CAT_DTLS_SEQ").ToString();
                    cmbCatStatus.SelectedValue = DataBinder.Eval(DataItem, "CAT_STATUS_SEQ").ToString();
                    // บุคลากร
                    cmbTmpEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "TMP_EMP_TEXT").ToString(), DataBinder.Eval(DataItem, "TMP_EMP_SEQ").ToString()));
                    cmbTmpEmployee.SelectedValue = DataBinder.Eval(DataItem, "TMP_EMP_SEQ").ToString();

                    //ตำแหน่งสายงาน
                    cmbFieldPosition.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "FIELD_POS_NAME").ToString(), DataBinder.Eval(DataItem, "FIELD_POS_SEQ").ToString()));
                    cmbFieldPosition.SelectedValue = DataBinder.Eval(DataItem, "FIELD_POS_SEQ").ToString();
                    
                    txtNo.Text = DataBinder.Eval(DataItem, "CAT_NO").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();
                }
                else
                {
                    
                }
            }
        }
        private void BindCatStatusCombo()
        {
            DataSet ds = catBLL.LoadCatalogDtlsStatus();
            DataTable dt = ds.Tables[0];

            cmbCatStatus.DataSource = dt;
            cmbCatStatus.DataBind();
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            catBLL.InsertDtls(Int32.Parse(Request.QueryString["ParentSeq"].ToString())
                , Int32.Parse(txtNo.Text)
                , Int32.Parse(cmbCatStatus.SelectedValue)
                , Int32.Parse(cmbTmpEmployee.SelectedValue)
                , Int32.Parse(cmbFieldPosition.SelectedValue)
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            catBLL.UpdateDtls(Int32.Parse(lblSequence.Text)
                , Int32.Parse(Request.QueryString["ParentSeq"].ToString())
                , Int32.Parse(txtNo.Text)
                , Int32.Parse(cmbCatStatus.SelectedValue)
                , Int32.Parse(cmbTmpEmployee.SelectedValue)
                , Int32.Parse(cmbFieldPosition.SelectedValue)
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbFieldPosition_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            DataSet ds = fieldBLL.LoadFieldExists("", "", Session["EMPTYPE"].ToString());
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbFieldPosition.Items.Add(new RadComboBoxItem(dt.Rows[i]["FIELD_POS_NAME"].ToString(), dt.Rows[i]["FIELD_POS_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdFieldName = new TableCell();
                Label lblFieldName = new Label();
                lblFieldName.Text = dt.Rows[i]["FIELD_POS_NAME"].ToString();
                tdFieldName.Width = Unit.Pixel(100);
                tdFieldName.Controls.Add(lblFieldName);
                tr.Cells.Add(tdFieldName);

                TableCell tdPosTypeName = new TableCell();
                Label lblPosTypeName = new Label();
                lblPosTypeName.Text = dt.Rows[i]["POS_TYPE_NAME"].ToString();
                tdPosTypeName.Width = Unit.Pixel(100);
                tdPosTypeName.Controls.Add(lblPosTypeName);
                tr.Cells.Add(tdPosTypeName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["CLASS_SPEC_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                ((Table)cmbFieldPosition.Items[j].FindControl("tableFieldPosition")).Controls.Add(tr);
                j++;
            }
        }

        protected void cmbTmpEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            DataSet ds = appBLL.LoadApplicants((int?)null, 1, (int?)null, (int?)null, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbTmpEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["TMP_EMP_TEXT"].ToString(), dt.Rows[i]["TMP_EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = dt.Rows[i]["TMP_EMP_TEXT"].ToString();
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);

                TableCell tdBirthDate = new TableCell();
                Label lblBirthDate = new Label();
                lblBirthDate.Text = string.Format("{0:dd-MM-yyyy}", dt.Rows[i]["BIRTH_DATE"]);
                tdBirthDate.Width = Unit.Pixel(100);
                tdBirthDate.Controls.Add(lblBirthDate);
                tr.Cells.Add(tdBirthDate);

                TableCell tdCitizen = new TableCell();
                Label lblCitizen = new Label();
                lblCitizen.Text = dt.Rows[i]["CITIZEN"].ToString();
                tdCitizen.Width = Unit.Pixel(100);
                tdCitizen.Controls.Add(lblCitizen);
                tr.Cells.Add(tdCitizen);

                ((Table)cmbTmpEmployee.Items[j].FindControl("tableTmpEmployee")).Controls.Add(tr);
                j++;
            }
        }
    }
}